import axios from 'axios'
import { getToken, removeToken } from '@/utils/cookie'
import { Notification, MessageBox, Message} from 'element-ui'

// 创建Axios实例
const axiosInstance = axios.create({
    baseURL: process.env.BASE_API, // API的base_url
    timeout: 5000,                 // 请求超时时间
})

// request拦截器
axiosInstance.interceptors.request.use(config => {
    // 发送请求前
    //config.headers['X-Token'] = 'token' // 设置默认携带的头信息
    return config
}, error => {
    // 请求错误
    console.log('request err: ' + error)
    return Promise.reject(error);
})

// response拦截器
axiosInstance.interceptors.response.use(
    response => {
        // 响应结果返回到页面前可以对数据进行处理，自定义code来标示请求状态，当code返回如下情况为权限有问题，登出并返回到登录页
        return response
    },
    error => {
        // 请求已发出，但服务器响应的状态码不在 2xx 范围内
        if(error.response) {
            switch(error.response.status) {
                case 0:
                    Msg('error', error.response.status + '网络暂时未能连通')
                    break
                case 400:
                    Msg('error', error.response.status + '错误的请求')
                    break
                case 412:
                    new MessageBox('未登录或登录超时，请重新登录', '提示', {
                        confirmButtonText: '重新登录',
                        type: 'warning'
                    }).then(() => {
                        location.reload()
                    }).catch(() => {
                        console.log('登出发生错误')
                    });

                    break
                case 401:
                case 403:
                    Msg('error', error.response.status + '  ' + error.response.data.message)
                    break
                case 404:
                    ExceptionMsg('info', error.response.status, '该资源不存在：' + error.config.url)
                    break
                case 405:
                    Msg('error', error.response.status + '请求的方式不正确')
                    break
                case 408:
                    Msg('error', error.response.status + '请求超时')
                    break
                case 500:
                    ExceptionMsg('', '服务器异常-'+error.response.status, error.response.data.message)
                    break
                case 503:
                    Msg('error', error.response.status+' '+error.response.data.message)
                    break
                case 504:
                    Msg('error', error.response.status + '服务器被吃了⊙﹏⊙∥')
                    break
                default:
                    break
            }
        } else {
            console.log('请求发生错误', error)
            Msg('error', '未知错误!')
        }

        /*if (error.response) {
            console.log('Error Msg', error.message);
            console.log('Error Code', error.response.status);
            console.log('Error data', error.response.data);
            console.log('Error Headers', error.response.headers);
        } else {
            console.log('Error Msg', error.message);
        }*/
        //console.log('Error Config', error.config);

        return Promise.reject(error)
    }
)

function Msg(type, message) {
    Message({
        type: type,
        duration: 2000,
        message: message
    })
}
function NotificationMsg(type, title, message) {
    Notification({
        type: type,
        duration: 2000,
        title: title,
        message: message
    })
}
function ExceptionMsg(type, title, message) {
    MessageBox({
        type: type,
        title: title,
        message: message,
        customClass: 'errorMessageBox'
    })
}
export default axiosInstance;